/**
 * Created by Administrator on 14-8-7.
 */

$(function(){

    $("#AddNewAddress").off().on("click",function(){
        address_updates('0',$(this).attr("data-type"));
    })
    $(document).off("click","#close").on("click","#close",function(e){
        e.stopPropagation();

        $("#dialog-modal").dialog("close");


    });
    $(document).off("click","#addBack").on("click","#addBack",function(e){
        e.stopPropagation();

        $("#dialog-modal").dialog("close");
        $("form[name='theFormAddress']").find($('input[type="reset"]')).click()

    });
    $(document).off("click",".address-edit").on("click",".address-edit",function(e){
        e.stopPropagation();
        var AddressId=$(this).attr("data-addressid");

        address_updates(AddressId,$(this).attr("data-type"));
    });
    $(document).off("click","#checkoutAddrList dl").on("click","#checkoutAddrList dl",function(){
        if($(this).hasClass("checked-on")){
            return;
        }else{
            $(this).addClass("checked-on").siblings().removeClass("checked-on");
            var address_id=$(this).attr("data-addressId");
            $.post('flow.php?step=select_consignee', 'address_id=' + address_id, addressResponse,  'JSON');
        }

    })
    $(document).off("click",".address-del").on("click",".address-del",function(e){
        e.stopPropagation();
        e.preventDefault();
        var addressId=$(this).attr("data-addressId");
        if (confirm('你确认要删除该收货地址吗？')){
            location.href='user.php?act=drop_consignee&id='+addressId+'&myactback='+$(this).attr("data-type");

        }
    });

    /*地址编辑*/
    function address_updates(address_id, myactback, names) {
        $.ajax({
            type: "get",
            dataType: "html",
            url: "/index.php?C=Order&F=path",
            data:"address_id="+address_id+"&myactback="+myactback,
            success: function(data){
                $("#dialog-modal").html(data).show().dialog({width: 512,height:360,modal: true,focus:function(ev,data){$('#addOk').focus();}});
            }
        });
    }
    /* *
     * 处理收货人信息选择的返回信息
     */
    function addressResponse(result)
    {
        if (result.error == 0)
        {
            if($('input:radio[name="inv_payee"]:checked').val() == '个人') {
                $('#inv_payee_info').val(result.name);
            }
            $('#chechout_address_bottom').html(result.message);
        }
    }


})

region.isAdmin = false;
var consignee_not_null = "收货人姓名不能为空！";
var country_not_null = "请您选择收货人所在国家！";
var province_not_null = "请您选择收货人所在省份！";
var city_not_null = "请您选择收货人所在城市！";
var district_not_null = "请您选择收货人所在区域！";
var invalid_email = "您输入的邮件地址不是一个合法的邮件地址。";
var address_not_null = "收货人的详细地址不能为空！";
var tele_not_null = "电话不能为空！";
var shipping_not_null = "请您选择配送方式！";
var payment_not_null = "请您选择支付方式！";
var goodsattr_style = "1";
var tele_invaild = "电话号码不有效的号码";
var zip_not_num = "邮政编码只能为6位数字";
var mobile_invaild = "手机号码不是合法号码";
var flow_no_payment = "您必须选定一个支付方式。";
var flow_no_shipping = "您必须选定一个配送方式。";

onload = function() {
    if (!document.all)
    {
       // document.forms['theFormAddress'].reset();
    }
}
/* *
 * 检查收货地址信息表单中填写的内容
 */
function checkConsignee(frm)
{
    var msg = new Array();
    var err = false;

    if (frm.elements['province'] && frm.elements['province'].value == 0 && frm.elements['province'].length > 1)
    {
        err = true;
        msg.push(province_not_null);
    }

    if (frm.elements['city'] && frm.elements['city'].value == 0 && frm.elements['city'].length > 1)
    {
        err = true;
        msg.push(city_not_null);
    }

    if (frm.elements['district'] && frm.elements['district'].length > 1)
    {
        if (frm.elements['district'].value == 0)
        {
            err = true;
            msg.push(district_not_null);
        }
    }

    if (Utils.isEmpty(frm.elements['consignee'].value))
    {
        err = true;
        msg.push(consignee_not_null);
    }



    if (frm.elements['address'] && Utils.isEmpty(frm.elements['address'].value))
    {
        err = true;
        msg.push(address_not_null);
    }

    if (frm.elements['zipcode'] && (frm.elements['zipcode'].value.length != 6 || (!Utils.isNumber(frm.elements['zipcode'].value))))
    {
        err = true;
        msg.push(zip_not_num);
    }

    if (!Utils.isMobile(frm.elements['mobile'].value))
    {
        err = true;
        msg.push(mobile_invaild);
    }

    if (err)
    {
        message = msg.join("\n");
        alert(message);
    }
    return ! err;
}